<?php

class OrdertrackingController extends Zend_Controller_Action
{

    public function init()
    {
        IndexController::initAdmin($this);
    }

    ////////////////////action function start//////////////////////////////
    public function indexAction()
    {
    		
    }
    
    public function listorderstatusAction()
    {
        $omsModel = new QOmslive();
        
        $arr_omsstatus = $omsModel->getStatusOms();        
        
        $result = array();
        foreach ($arr_omsstatus as $status)
        {
        	$displayStatusId = $status['id_status_display'];
        	$result[$displayStatusId]['enable_notification'] = $status['enable_notification'];
        	$result[$displayStatusId]['mail_template'] = ($status['mail_template']);
        	$result[$displayStatusId]['text'] = $status['text'];
        	$result[$displayStatusId]['order_item_status'][] = $status['fk_id_sales_order_item_status'];
        }
        
        // parse order_item_status to string
        foreach($result as &$status)
        {        	
        	$order_item_status = $status['order_item_status'];
        	$status['order_item_status_string'] = implode(',', $order_item_status);
        }

        $this->view->orderstatus = $result;
    }
    
   public function updatestatusAction()
   {
   		$this->_helper->viewRenderer->setNorender();   		
   		$data = $_GET;
   		
   		   		$omsModel = new QOmslive();
   		$display_status_data = array();
   		$display_status_data['text'] = isset($data['status_text']) ? $data['status_text'] : null;
   		
   		$display_status_id = isset($data['status_id']) && !empty($data['status_id']) ? $data['status_id'] : null;
   		
   		if($display_status_id == null)
   		{
   			$display_status_id = $omsModel->insertDisplayStatus($display_status_data);  						
   		}
   		
   		$display_status_data['enable_notification'] = isset($data['enable_notification']) ? $data['enable_notification'] : 0;
   		$display_status_data['mail_template'] = isset($data['oms_email_template']) ? $data['oms_email_template'] : null;
   		$omsModel->updateDisplayStatus($display_status_data, $display_status_id);
   		
   		// update oms status mappeer
   		$omsModel->deleteOmsStatusMapper($display_status_id);
   		
   		$oms_status_string =  $data['oms_status'];
   		$arr_oms_status = explode(',', $oms_status_string);   		
   		foreach ($arr_oms_status as $oms_status_id)
   		{
   			$oms_status_id = intval($oms_status_id);
   			$oms_status['fk_id_status_display'] = $display_status_id;
   			$oms_status['fk_id_sales_order_item_status'] = $oms_status_id;
   			$omsModel->insertOmsStatusMapper($oms_status);
   		}
   		
   		// respone
   		$result = array('err' => 0, 'display_status_id' => $display_status_id);
   		echo json_encode($result);
   }
   
   public function deletestatusAction()
   {
   		$this->_helper->viewRenderer->setNorender();
   		$data = $_GET;
   		$displayStatusId = $data['display_status_id'];
   		$omsModel = new QOmslive();
   		$omsModel->deleteDisplayStatus($displayStatusId);
   		$omsModel->deleteOmsStatusMapper($displayStatusId);
   		
   		// respone
   		$result = array('err' => 0);
   		echo json_encode($result);
   }
}

?>
